package org.hyl.javaext.disruptor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

public class DisruptorMain {
    public static void main(String[] args) {
        DisruptorFactory factory = new DisruptorFactory();          // ����
        ExecutorService executor = Executors.newCachedThreadPool(); // �̳߳�
        int BUFFER_SIZE = 16;   // ����Ϊ2����ָ��

        // ��ʼ��Disruptor
        @SuppressWarnings("deprecation")
		Disruptor<FileData> disruptor = new Disruptor<FileData>(factory,
                BUFFER_SIZE,
                executor,
                ProducerType.MULTI,         // Create a RingBuffer supporting multiple event publishers to the one RingBuffer
                new BlockingWaitStrategy()  // Ĭ����������
                );
        // ����������
        //�ֿ�д�����һ��д������
        //s1||s2 ���ͬһ���������� ��ͬά���¼�,һ���¼�ֻ��������һ������������,��������
        disruptor.handleEventsWithWorkerPool(new DisruptorConsumer(2),
                new DisruptorConsumer(1)
        );
        // ���¿���һ�������߳� s3
        disruptor.handleEventsWithWorkerPool(new DisruptorConsumer(3)
              
        );
        // ÿ���˶��쵽��ͬ��������
        // �ֿ�д�����һ��дû������ c1,c2
        disruptor.handleEventsWith(new DisruptorConsumer2(1),new DisruptorConsumer2(2));
        // ���¿���һ�������߳� c3
        disruptor.handleEventsWith(new DisruptorConsumer2(3));
        disruptor.start();
        // ����������
        RingBuffer<FileData> ringBuffer = disruptor.getRingBuffer();
        DisruptorProducer producer = new DisruptorProducer(ringBuffer);
        producer.read("demo.txt");

        // �ر�
        disruptor.shutdown();
        executor.shutdown();
    }
}